package com.gpy.hospital.cms.controller;

import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.pojo.User;
import com.gpy.hospital.cms.service.IUserService;
import com.gpy.hospital.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author
 * @Date 2020/2/26 21:58
 * @Desc
 **/
@Controller
public class UserController {

    @Autowired
    private IUserService userService;


    @GetMapping("/cms/user/list")
    @ResponseBody
    public ServerResponse userList(@RequestParam(value = "userEmail", defaultValue = "") String userEmail,
                                   @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        PageInfo<User> pageInfo = userService.getByEmail(userEmail, pageNum, pageSize);
        return ServerResponse.createBySuccess("查询成功！", pageInfo.getList(), pageInfo.getTotal());
    }


    @GetMapping("/cms/userEditPage")
    public String userEditPage(@RequestParam("userId") Integer userId, Model model) {
        User user = userService.getById(userId);
        model.addAttribute("UserInfo", user);
        return "/cms/userEditPage";
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @PostMapping("/cms/user/update")
    @ResponseBody
    public ServerResponse updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @GetMapping("/cms/user/delete")
    @ResponseBody
    public ServerResponse delete(@RequestParam("userId") Integer userId) {
        return userService.deleteById(userId);
    }

    @GetMapping("/reception/login/page")
    public String loginPage() {
        return "/reception/login";
    }


    @GetMapping("/reception/register/page")
    public String registerPage() {
        return "/reception/register";
    }


    @PostMapping("/reception/login")
    @ResponseBody
    public ServerResponse login(HttpServletRequest request, User user) {
        return userService.login(request, user);
    }

    @PostMapping("/reception/register")
    @ResponseBody
    public ServerResponse register(User user) {
        return userService.register(user);
    }

    @GetMapping("/reception/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "/reception/login";
    }

    @GetMapping("/reception/profile")
    public String receptionProfile() {
        return "/reception/profile";
    }

    @PostMapping("/reception/user/update")
    @ResponseBody
    public ServerResponse userUpdate(User user) {
        return userService.updateUser(user);
    }

    @GetMapping("/admin/login/page")
    public String adminLoginPage() {
        return "/cms/login";
    }

    @PostMapping("/admin/login")
    @ResponseBody
    public ServerResponse adminLogin(HttpServletRequest request, User user) {
        return userService.adminLogin(request, user);
    }

    @GetMapping("/admin/cms/index")
    public String adminCmsIndex() {
        return "/cms/adminCmsIndex";
    }

    @PostMapping("/cms/user/add")
    @ResponseBody
    public ServerResponse cmsUserAdd(@RequestBody User user) {
        return userService.addUser(user);
    }

}
